
 /**
 * @param {number[]} nums
 * @return {number[][]}
 */
var threeSum = function(nums) {
    nums.sort((a, b) => a-b)
    var slow
    var l
    var r
    var sum
    var result = []
    for (slow=0; slow<nums.length-2; slow++) {
        if (nums[slow] > 0) break
        if (slow > 0 && nums[slow] === nums[slow-1]) continue
        l = slow + 1
        r = nums.length -  1
        while (l < r) {
            sum = nums[slow] + nums[l] + nums[r]
            if (sum === 0) {
                result.push([nums[slow], nums[l], nums[r]])
                while (l < r && nums[l] === nums[++l])
                    ;
                while (l < r && nums[r] === nums[--r])
                    ;
            } else if (sum > 0) {
                while (l < r && nums[r] === nums[--r])
                    ;
            } else {
                while (l < r && nums[l] === nums[++l])
                    ;
            }
        }
    }
    return result
};

console.log(threeSum([-1,0,1,2,-1,-4]))